<?php
declare (strict_types=1);

namespace app\admin\service\finance;

use app\admin\logic\finance\CreditConfig as CreditConfigLogic;
use app\admin\logic\system\Config as SystemConfig;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\page\PageForm;
use think\Request;

/**
 * 通用积分配置
 */
class CreditConfig extends BaseService
{
    /**
     * 通用积分配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-21
     *
     * @return   [type]                         [description]
     */
    public function config(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();

            app(CreditConfigLogic::class)->batchSetting($data);

            return $this->parseToData([], 1, '保存成功');
        }
        $fields        = [
            // 注册
            'register'        => FormItem::make()->title('注册')->slot('积分')->css(['width' => '146px'])->required(),
            // 推荐注册
            'invite_register' => FormItem::make()->title('推荐注册')->slot('积分')->css(['width' => '146px'])->required(),
            // 签到
            'sign_in'         => FormItem::make()->title('签到')->slot('积分')->css(['width' => '146px'])->required(),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->datas   = CreditConfigLogic::where('mhm_id', request()->mhm_id)->whereIn('key', ['register', 'invite_register', 'sign_in'])->column('score', 'key');

        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/admin/finance/credit/config';

        return $form->send();

    }

    /**
     * 充值积分比例配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-21
     *
     * @return   [type]                         [description]
     */
    public function recharge()
    {
        $fields        = [
            // 比例
            'ratio' => FormItem::make('custom')->title('充值比例')->help('充值比例用于自定义充值积分'),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $key             = 'credit.recharge';
        $config          = SystemConfig::get($key, ['ratio' => '1:100'], request()->mhm_id);
        $config['__key'] = $key;
        $form->datas     = $config;

        return $form->send();

    }

    /**
     * 积分抵扣比例配置
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-08-25
     *
     * @return [type] [description]
     */
    public function redeem()
    {
        $fields        = [
            // 比例
            'ratio' => FormItem::make('custom')->title('兑换比例')->help('人民币与积分的兑换比例'),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $key             = 'credit.redeem';
        $config          = SystemConfig::get($key, ['ratio' => '1:100'], request()->mhm_id);
        $config['__key'] = $key;
        $form->datas     = $config;

        return $form->send();

    }

    /**
     * 订单满抵配置
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-09-06
     */
    public function order()
    {
        $fields        = [
            'order' => FormItem::make('inputNumber')->title('订单满抵')->min(0.01)->precision(2)->required()->help('订单金额大于等于该金额可使用积分抵扣'),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $key             = 'credit.order';
        $config          = SystemConfig::get($key, ['order' => '1'], request()->mhm_id);
        $config['__key'] = $key;
        $form->datas     = $config;

        return $form->send();

    }

    /**
     * 积分满用配置
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-09-06
     */
    public function threshold()
    {
        $fields        = [
            'threshold' => FormItem::make('inputNumber')->title('积分满用')->min(1)->required()->help('账户积分金额大于等于该金额可使用积分抵扣'),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $key             = 'credit.threshold';
        $config          = SystemConfig::get($key, ['threshold' => '1'], request()->mhm_id);
        $config['__key'] = $key;
        $form->datas     = $config;

        return $form->send();

    }
}
